﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using BusinessLayer;
using PayRoll.Datasets;
using BusinessLayer.DTO;
using CrystalDecisions.Shared;

namespace PayRoll
{
    public partial class Report : Form
    {
        public Report()
        {
            InitializeComponent();
        }

        private void Test_Load(object sender, EventArgs e)
        {

        }
        public void LoadSalaryReport(int salaryProcessID, int employeeID)
        {
            ReportDocument rdPckCost = new ReportDocument();
            rdPckCost.Load(@"Reports\Salaryslip.rpt");
            ClsSalaryGeneration ObjClsSalaryGeneration = new ClsSalaryGeneration();
            bool chkDeduct = false;
            if (ObjClsSalaryGeneration.IsSalaryGenerated(salaryProcessID, employeeID))
            {
                ObjClsSalaryGeneration.GetAllDetailsForEmployee(employeeID, salaryProcessID, ref chkDeduct, true);
            }
            SalarySlip.EmployeeDataTable Employee = new SalarySlip.EmployeeDataTable();
            SalarySlip.EmployeeRow empDataRow = Employee.NewEmployeeRow();
            empDataRow.Name = ObjClsSalaryGeneration.employeeInfo.FullName;
            empDataRow.Department = ObjClsSalaryGeneration.employeeInfo.Department;
            empDataRow.Designation = ObjClsSalaryGeneration.employeeInfo.Designation;
            empDataRow.EmploymentStatus = ObjClsSalaryGeneration.employeeInfo.EmploymentStatus;
            empDataRow.EmploymentType = ObjClsSalaryGeneration.employeeInfo.EmploymentName;
            empDataRow.OverTimeRate = ObjClsSalaryGeneration.employeeInfo.OverTimeRate.ToString();
            Employee.Rows.Add(empDataRow);

            List<SalaryGenarationInfo> SalaryGenarationInfo = new List<SalaryGenarationInfo>();
            SalaryGenarationInfo = ObjClsSalaryGeneration.SalaryGenarationInfo;
            List<SalaryGenarationInfo> SalaryGenarationInfoDetails = SalaryGenarationInfo.Where(x => x.SalaryEnum != Utility.SalaryEnum.TotalSalary).ToList();

            SalarySlip.SalaryStructuresDataTable salaryDetailsDataTable = new SalarySlip.SalaryStructuresDataTable();
            foreach (SalaryGenarationInfo item in SalaryGenarationInfoDetails)
            {
                SalarySlip.SalaryStructuresRow dataRow = salaryDetailsDataTable.NewSalaryStructuresRow();
                if (item.SalaryEnum == Utility.SalaryEnum.Basics)
                {
                    dataRow.Type = "Basics";
                }
                if (item.SalaryEnum == Utility.SalaryEnum.Deductables)
                {
                    dataRow.Type = "Deductions";
                }
                if (item.SalaryEnum == Utility.SalaryEnum.OtherIncomes)
                {
                    dataRow.Type = "Other Incomes";
                }

                //dataRow.Type = item.SalaryEnum.ToString();
                dataRow.Head = item.SalaryHead;
                dataRow.Amount = item.Amount;
                dataRow.Deduct_Amount = item.Decuctions;
                dataRow.ActualAmount = item.ActualAmount;
                dataRow.Deducted = item.DeductSalaryIfAbsent;// == "Yes" ? "Deducted" : "Not Deducted";
                dataRow.Debit = item.Debit == "Yes" ? "Debit" : "Credit";
                salaryDetailsDataTable.Rows.Add(dataRow);
            }


            List<SalaryGenarationInfo> SalaryGenarationInfoTotalIncome = SalaryGenarationInfo.Where(x => x.SalaryEnum == Utility.SalaryEnum.TotalSalary).ToList();
            SalaryGenarationInfo TotalSalaryRow = SalaryGenarationInfoTotalIncome.FirstOrDefault();
            SalarySlip.OtherParameterDataTable other = new SalarySlip.OtherParameterDataTable();
            SalarySlip.OtherParameterRow otherDataRow = other.NewOtherParameterRow();
            otherDataRow.ToDate = Utility.GetDateToDisplay(ObjClsSalaryGeneration.toDate);
            otherDataRow.FromDate = Utility.GetDateToDisplay(ObjClsSalaryGeneration.fromdate);
            otherDataRow.TotalDays = ObjClsSalaryGeneration.TotalDays.ToString();
            otherDataRow.TotalNoOfWorkingdays = ObjClsSalaryGeneration.TotalNoOfWorkingdays.ToString();
            otherDataRow.TotalDaysWorked = ObjClsSalaryGeneration.TotalDaysWorked.ToString();
            otherDataRow.TotalDaysOff = ObjClsSalaryGeneration.TotalDaysOff.ToString();
            otherDataRow.DaysWithOverTime = ObjClsSalaryGeneration.DaysWithOverTime.ToString();
            otherDataRow.OverTimeWorked = ObjClsSalaryGeneration.OverTimeWorked.ToString();
            otherDataRow.SalaryPeriod = "Salary for the period of " + Utility.GetDateToDisplay(ObjClsSalaryGeneration.fromdate) + " to " + Utility.GetDateToDisplay(ObjClsSalaryGeneration.toDate);
            otherDataRow.TotalSalary = TotalSalaryRow.Amount;
            otherDataRow.TotalDeductions = TotalSalaryRow.Decuctions;
            otherDataRow.TotalAmount = TotalSalaryRow.ActualAmount;
            otherDataRow.ActualAmountInWords = BahdlComponentLib.CommonFunction.ConvertToWords(Convert.ToDouble(TotalSalaryRow.ActualAmount), "INR");
            other.Rows.Add(otherDataRow);

            DataSet ds = new DataSet();
            ds.Tables.Add(Employee);
            ClsCompanyInfo clsCompanyInfo = new ClsCompanyInfo();
            ds.Tables.Add(clsCompanyInfo.GetAllCompanyInfo());
            ds.Tables.Add(other);
            ds.Tables.Add(salaryDetailsDataTable);
            rdPckCost.SetDataSource(ds);
            crystalReportViewer1.ReportSource = rdPckCost;
        }
        public void PerEmployeePaymentReport(int employeeID, DateTime fromDate, DateTime toDate)
        {
            ReportDocument rdPckCost = new ReportDocument();
            rdPckCost.Load(@"Reports\PaymentPerEmployee.rpt");
            ClsSalaryGeneration ObjClsSalaryGeneration = new ClsSalaryGeneration();
            DataTable paySlips = ObjClsSalaryGeneration.GetSalaryPaidPerEmployee(employeeID, fromDate, toDate);
            SalarySlip.PaySlipsDataTable PaySlips = new SalarySlip.PaySlipsDataTable();
            foreach (DataRow item in paySlips.Rows)
            {
                SalarySlip.PaySlipsRow row = PaySlips.NewPaySlipsRow();
                row.GenerationDate = item.Field<DateTime>("GenerationDate");
                row.Amount = item.Field<decimal>("Amount");
                row.Deductions = item.Field<decimal>("Deductions");
                row.ActualAmount = item.Field<decimal>("ActualAmount");
                PaySlips.Rows.Add(row);
            }

            ClsEmployee objEmployee = new ClsEmployee();
            objEmployee.GetEmployee(employeeID);

            SalarySlip.EmployeeDataTable Employee = new SalarySlip.EmployeeDataTable();
            SalarySlip.EmployeeRow empDataRow = Employee.NewEmployeeRow();
            empDataRow.Name = objEmployee.FullName;
            empDataRow.Department = objEmployee.Department;
            empDataRow.Designation = objEmployee.Designation;
            empDataRow.EmploymentStatus = objEmployee.EmploymentStatus;
            empDataRow.EmploymentType = objEmployee.EmploymentName;
            empDataRow.OverTimeRate = objEmployee.OverTimeRate.ToString();
            Employee.Rows.Add(empDataRow);
            DataSet ds = new DataSet();
            ds.Tables.Add(Employee);
            ClsCompanyInfo clsCompanyInfo = new ClsCompanyInfo();
            DataTable CompanyDT = clsCompanyInfo.GetAllCompanyInfo();
            CompanyDT.Columns.Add("ReportDescriptionLine");
            CompanyDT.Rows[0]["ReportDescriptionLine"] = "Payment report from " + Utility.GetDateToDisplay(fromDate) + " to " + Utility.GetDateToDisplay(toDate);
            ds.Tables.Add(CompanyDT);
            ds.Tables.Add(PaySlips);

            rdPckCost.SetDataSource(ds);
            crystalReportViewer1.ReportSource = rdPckCost;
        }
    }
}
